const REG_PHONE = /^\d{11}$/ // 手机号
const REG_VCODE = /^\d{6}$/ // 验证码
const REG_CARDNUMBER = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ // 身份证号
const REG_CHINESE_NAME = /^[\u4E00-\u9FA5]{2,4}$/ // 中文姓名
const REG_PASSWORD = /^[0-9A-Za-z]{6,20}$/

export function testPhone (v) {
  return REG_PHONE.test(v)
}

export function testVcode (v) {
  return REG_VCODE.test(v)
}

export function testCardNumber (v) {
  return REG_CARDNUMBER.test(v)
}

export function testPassword (v) {
  return REG_PASSWORD.test(v)
}
export function testEmpty (v) {
  return String(v).trim() === '' || v === null
}

export function testChineseName (v) {
  return REG_CHINESE_NAME.test(v)
}
/**
 * 根据身份证号码获取出生日期
 * @param {String} idCard
 */
export function getBirthdayFromIdCard (idCard) {
  let birthday = ''
  if (idCard != null && idCard !== '') {
    if (idCard.length === 15) {
      birthday = '19' + idCard.substr(6, 6)
    } else if (idCard.length === 18) {
      birthday = idCard.substr(6, 8)
    }

    birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-')
  }
  return birthday
}
export function getSexFromIdCard (idCard) {
  let sex = '1'
  if (idCard != null && idCard !== '') {
    if (idCard.length === 15) {
      sex = idCard.substr(14, 1) % 2 === 1 ? '1' : '2'
    } else if (idCard.length === 18) {
      sex = idCard.substr(16, 1) % 2 === 1 ? '1' : '2'
    }
  }
  return sex
}
/**
 * 格式化日期
 * @param {Date} date
 * @param {String} fmt 'yyyy-MM-dd hh:mm:ss'
 */
export function formatDate (date, fmt) {
  let padLeftZero = str => ('00' + str).substr(str.length)
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
    }
  }
  return fmt
}
export function leftTime (time1, time2) {
  let syhm = (time1 - time2)
  let day = 0
  let hour = 0
  let minite = 0
  let second = 0
  if (syhm > 0) {
    day = Math.floor(syhm / 1000 / 60 / 60 / 24)
    hour = Math.floor(syhm / 1000 / 60 / 60 % 24)
    minite = Math.floor(syhm / 1000 / 60 % 60)
    second = Math.floor(syhm / 1000 % 60)
  }
  return {
    day,
    hour,
    minite,
    second
  }
}
export function getCurrentDate () {
  let weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  let d = new Date()
  let d2 = formatDate(d, 'yyyy年MM月dd日 ')
  return d2 + weeks[d.getDay()]
}
export function getWeekName (week) {
  let weeks = ['', '周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return weeks[week]
}
export function objectDate (date) {
  if (date && typeof date === 'string') {
    date = new Date(date)
    let o = {
      'Y': date.getFullYear(),
      'M': date.getMonth() + 1,
      'D': date.getDate()
    }
    return o
  }
  return date
}
export function deepCopy (obj) {
  return JSON.parse(JSON.stringify(obj))
}
export function GetRequest (to) {
  let url = to || ''
  let theRequest = {}
  if (url.indexOf('?') !== -1) {
    let str = url.split('?')[1]
    let strs = str.split('&')
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
    }
  }
  return theRequest
}
export function getWeekStartAndEnd (d) {
  let now = new Date()
  let nowTime = now.getTime()
  let day = now.getDay()
  let oneday = 60 * 60 * 24 * 1000
  let sunday = nowTime - (-1 * d + day) * oneday
  let satday = nowTime + (d + 6 - day) * oneday
  return {
    start: formatDate(new Date(sunday), 'yyyy-MM-dd'),
    end: formatDate(new Date(satday), 'yyyy-MM-dd')
  }
}
export function DateDiff (sDate1, sDate2) {
  var aDate, oDate1, oDate2, iDays
  aDate = sDate1.split('-')
  oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
  aDate = sDate2.split('-')
  oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
  iDays = parseInt((oDate1 - oDate2) / 1000 / 60 / 60 / 24) // 把相差的毫秒数转换为天数
  return iDays
}
export function getAgeFromBirthday (birthday) {
  if (!birthday) {
    return 0
  }
  let bornYear = birthday.substring(0, 4)
  let curYear = new Date().getFullYear()
  return curYear - bornYear
}
export function isWechatBrowser () {
  let ua = navigator.userAgent.toLowerCase()
  return ua.indexOf('micromessenger') > -1
}
export const browser = {
  versions: (function () {
    var u = navigator.userAgent
    return {
      trident: u.indexOf('Trident') > -1, // IE内核
      presto: u.indexOf('Presto') > -1, // opera内核
      webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
      gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1, // 火狐内核
      mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
      ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
      android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, // android终端
      iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
      iPad: u.indexOf('iPad') > -1, // 是否iPad
      webApp: u.indexOf('Safari') === -1, // 是否web应该程序，没有头部与底部
      weixin: u.indexOf('MicroMessenger') > -1, // 是否微信 （2015-01-22新增）
      qq: u.match(/\sQQ/i) === ' qq' // 是否QQ
    }
  }()),
  language: (navigator.browserLanguage || navigator.language).toLowerCase()
}
export const formatDateGetAge = (now) => {
  let Y = now.getFullYear()
  let year = new Date().getFullYear()
  return year - Y
}
